package com.weipai.demo.controller;

import com.weipai.demo.enums.RetCode;
import com.weipai.demo.model.PictureShow;
import com.weipai.demo.service.FileUploadService;
import com.weipai.demo.service.PictureService;
import com.weipai.demo.service.PicturnService;
import com.weipai.demo.util.LayuiTablePagingResult;
import com.weipai.demo.util.Response;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;

/**
 * @Author: 赵项阳
 * @Date: 2019/6/21 18:35
 * @Version 1.0
 */
@RestController
public class PicturnController {

    @Autowired
    private PicturnService picturnService;
    @Autowired
    private FileUploadService fileUploadService;
    @Autowired
    private PictureService pictureService;


    //添加或修改广告位
    @RequestMapping(value = "/advertising/locationSave")
    public Response locationSave(String advertisingLocation,Integer status,Integer id){
      return picturnService.locationSave(advertisingLocation,status,id);
    }


    //删除广告位
    @RequestMapping(value = "/advertising/deleByid")
    public Response deleByid(Integer id){
        return picturnService.deleByid(id);
    }

    //展示广告位
    @RequestMapping(value = "/advertising/show")
    public Response show(){
        return picturnService.show();
    }

    //==================================================================================================================


    //广告图上传
    @RequestMapping(value = "/picture/uploadingImg", method = RequestMethod.POST)
    public Response uploadingImg(HttpServletRequest request, PictureShow pictureShow, MultipartFile img) {
        Response response = new Response();
        if (img != null) {
            String result = fileUploadService.saveImgUpload(request, img);
            if (result.equals("formatError")) {
                response.setCode(RetCode.IMG_FORMAT);
            } else if (result.equals("sizeover")) {
                response.setCode(RetCode.IMG_SIZE);
            } else if (result.equals("error")) {
                response.setCode(RetCode.IMG_ERROR);
            } else {
                pictureShow.setPictureUrl(result);
                response = pictureService.savePicture(pictureShow);
            }
        }else {
            pictureShow.setPictureUrl(pictureShow.getPictureUrl());
            response = pictureService.savePicture(pictureShow);
        }
        return response;

    }

    //广告图展示
    @RequestMapping(value = "/picture/pictureShow")
    public LayuiTablePagingResult pictureShow(Integer page, Integer size,Integer pictureClass){
        return pictureService.pictureShow(page,size,pictureClass);
    }


    //删除广告图
    @RequestMapping(value = "/picture/delpicture")
    public Response delpicture(Integer id){
        return pictureService.delpicture(id);
    }
}
